package com.blog.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blog.entity.Article;
import com.blog.entity.Category;
import com.blog.vo.CategoryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    @Select("SELECT * FROM tb_article WHERE user_id = #{currentId}")
    List<Article> selectByUid(@Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_article WHERE user_id = #{currentId} and title like CONCAT('%',#{value},'%')")
    List<Article> selectByUidAndValue(@Param("currentId") Long currentId, @Param("value") String value);

    @Select("SELECT * FROM tb_article WHERE id = #{id}")
    Article selectByUidAndId(@Param("id") Long id);

    @Insert("insert into " +
            "tb_article(cover, title,short_info, article_info, is_recommend,create_time, user_id) " +
            "VALUES (#{cover},#{title},#{shortInfo},#{articleInfo},0,#{createTime},#{userId})")
    void insert(Article article);


    void update(Article article);

    @Delete("delete from tb_article where id = #{id}")
    void deleteById(@Param("id") Long id);

    //自定义分页器
    List<Article> selectArticles(@Param("page") IPage<Article> page, @Param("ew") Wrapper<Article> queryWrapper);

    @Select("select count(*) from tb_article where user_id = #{currentId}")
    Long countArticle(@Param("currentId") Long currentId);


    @Select("SELECT * FROM tb_article as a WHERE a.id NOT IN (SELECT c.article_ID FROM tb_category_article as c) and user_id = #{currentId}")
    List<Article> articleByNoCategory(@Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_article WHERE id in (SELECT article_ID FROM tb_category_article WHERE category_id = #{categoryId}) and user_id = #{currentId}")
    List<Article> articleByCategory(@Param("categoryId") Long categoryId,@Param("currentId")  Long currentId);

    @Select("SELECT * FROM tb_category " +
            "WHERE id IN (SELECT category_id FROM tb_category_article WHERE article_ID = #{articleId}) " +
            "AND id IN (SELECT category_id FROM tb_user_category WHERE user_id = #{currentId});")
    List<Category> getCategory(@Param("articleId") Long articleId,@Param("currentId") Long currentId);

    @Select("SELECT * FROM tb_article WHERE user_id = #{uid} and is_recommend = #{IsRecommend}")
    List<Article> recommendSelect(@Param("uid")Long uid,@Param("IsRecommend")Long IsRecommend);

    /**
     * 由于你想要获取 create_time 最新的五篇文章，
     * 你需要使用 ORDER BY 子句对 create_time 进行降序排序，
     * 并使用 LIMIT 子句来限制结果集的数量。
     */
    @Select("SELECT * FROM tb_article WHERE user_id = #{uid} ORDER BY create_time DESC LIMIT 5")
    List<Article> timeNewSelect(Long uid);

    @Select("SELECT *  FROM tb_category WHERE id in (SELECT category_id FROM tb_category_article WHERE article_ID = #{aid})")
    List<CategoryVO> getCategoryByArticle(@Param("aid") Long aid);


}
